package com.chinasoft.mall.activity.subActivity.more;

import android.R.layout;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.AboutUsActivity;
import com.chinasoft.mall.activity.BaseActivity;
import com.chinasoft.mall.activity.CollectListActivity;
import com.chinasoft.mall.activity.LoadingActivity;
import com.chinasoft.mall.activity.MainActivity;
import com.chinasoft.mall.activity.PushMessageActivity;
import com.chinasoft.mall.framework.Config;
import com.chinasoft.mall.framework.ControllerCenter;
import com.chinasoft.mall.framework.OrderGoods;
import com.chinasoft.mall.model.BaseBean;
import com.chinasoft.mall.model.response.LoginCheckBean;
import com.chinasoft.mall.model.response.UpdateBean;
import com.chinasoft.mall.service.UpdateService;
import com.chinasoft.mall.util.LocalDataCommend;
import com.chinasoft.mall.widget.MySlipSwitch;
import com.chinasoft.mall.widget.MySlipSwitch.OnSwitchListener;

public class HomePageMoreActivity extends BaseActivity {

	private boolean isSwitchOn;

	private BaseBean goodsbyclass_bb;

	private RelativeLayout pushMessage;

	private RelativeLayout collectCenter;

	private RelativeLayout checkUpdate;

	private RelativeLayout aboutUs;
	
	private RelativeLayout logout;

	private BaseBean update;

	private static final String PUSH_MESSAGE = "推送消息";

	private MySlipSwitch mss;
	
	private BaseBean loginBean;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.homepage_more);
		mss = (MySlipSwitch) findViewById(R.id.myslipswitch);
		
		SharedPreferences mSharedPreferences = getSharedPreferences("switch",
				Context.MODE_PRIVATE);
		isSwitchOn = mSharedPreferences.getBoolean("value", true);
		
		SharedPreferences mSharedPreferences2 = getSharedPreferences("login", Context.MODE_PRIVATE);
		boolean tag = mSharedPreferences2.getBoolean("value", false);
		
		
		mss.setSwitchState(isSwitchOn);
		
		
		mss.setImageResource(R.drawable.bkg_switchon, R.drawable.bkg_switchoff,
				R.drawable.btn);

		mss.setOnSwitchListener(new onTouchImpl());

		pushMessage = (RelativeLayout) findViewById(R.id.pushmessage);
		pushMessage.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				goodsbyclass_bb = new BaseBean(Config.PUSH_MESSAGE);
				goodsbyclass_bb.reqData.put("page", "1");
				goodsbyclass_bb.reqData.put("rowCount", "10");
				goodsbyclass_bb.setUrl_type((byte) 1);
				control.sendMessage(goodsbyclass_bb);
			}
		});

		collectCenter = (RelativeLayout) findViewById(R.id.collectcenter);
		collectCenter.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (LocalDataCommend
						.getGoodsListBean(HomePageMoreActivity.this).size() < 1) {
					new AlertDialog.Builder(HomePageMoreActivity.this)
							.setIcon(R.drawable.alert_dialog_icon)
							.setTitle("暂无收藏")
							.setPositiveButton("确定",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int whichButton) {

											/* User clicked OK so do some stuff */
										}
									}).create().show();
				} else {
					((MainActivity) HomePageMoreActivity.this.getParent())
							.setCurrentActivity(CollectListActivity.class, "收藏");
				}
			}
		});

		aboutUs = (RelativeLayout) findViewById(R.id.aboutUs);
		aboutUs.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				((MainActivity) HomePageMoreActivity.this.getParent())
						.setCurrentActivity(AboutUsActivity.class, "aboutus",true);
			}
		});

		checkUpdate = (RelativeLayout) findViewById(R.id.checkUpdate);
		checkUpdate.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				update = new BaseBean(Config.UPDATE);
				update.setUrl_type((byte) 2);
				control.postMessage(update);
			}
		});
		
		logout = (RelativeLayout) findViewById(R.id.logout);
		if(tag){
			logout.setVisibility(View.VISIBLE);
		}
		logout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SharedPreferences mSharedPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);    
				Editor editor = mSharedPreferences.edit();           
				editor.putBoolean("value", false);
				editor.commit();
				new Handler().postDelayed(new Runnable() {
					
					@Override
					public void run() {
						Toast.makeText(HomePageMoreActivity.this, "注销成功", 500).show();
						HomePageMoreActivity.this.findViewById(R.id.logout).setVisibility(View.GONE);
					}
				}, 1000);
			}
		});

	}

	@Override
	public void success(BaseBean obj) {
		super.success(obj);
		if (obj.equals(goodsbyclass_bb)) {
			((MainActivity) HomePageMoreActivity.this.getParent())
					.setCurrentActivity(PushMessageActivity.class,
							PUSH_MESSAGE, obj);
		} else if (obj.equals(update)) {
			UpdateBean _ub = ((UpdateBean) update.getResData().getData());
			if (_ub != null && _ub.getUrl() != null && !"".equals(_ub.getUrl())) {
				callUpdateDialog("1", _ub.getUrl(), null);
			} else {
				callDralog();
			}
		}

	}

	private void callDralog() {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("提示").setMessage("您已是最新版本")
				.setPositiveButton("好", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();

					}
				}).show();

	}

	private void callUpdateDialog(String type, String url, String updateMessage) {
		final String updateUrl = url;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		// if ("2".equals(type)) {
		// builder.setTitle("升级提示")
		// .setMessage("您的版本过旧，需要升级才能运行程序！")
		// .setPositiveButton("现在升级",
		// new DialogInterface.OnClickListener() {
		// public void onClick(DialogInterface dialog,
		// int whichButton) {
		// Intent intent = new Intent();
		// intent.putExtra("url", updateUrl);
		// intent.setClass(HomePageMoreActivity.this,
		// UpdateService.class);
		// startService(intent);
		// dialog.dismiss();
		// }
		// }).show();
		// } else

		if ("1".equals(type)) { // 硬代码 ，字符串
			builder.setTitle("升级提示")
					.setMessage(
							updateMessage == null ? "发现新版本，是否立刻升级？"
									: updateMessage)
					.setPositiveButton("现在升级",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									System.out
											.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$1");
									dialog.dismiss();
									Intent intent = new Intent();
									intent.putExtra("url", updateUrl);
									intent.setClass(HomePageMoreActivity.this,
											UpdateService.class);
									startService(intent);
								}
							})
					.setNegativeButton("以后再说",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
								}
							}).show();
		}

	}

	class onTouchImpl implements OnSwitchListener {

		@Override
		public void onSwitched(boolean isSwitchOn) {
			OrderGoods.setState(isSwitchOn);
		}

	}
}
